#此扩展用于的模块扩展处理,用于支撑模块的
#   SUPPORT_3RD_PKG_NAME:
#       用于标准后期适配库的包名称
#       EWX里后期脚本使用变量都与此变量有关系
#       查找包的名称可与之相同也可以不同
#   EWX_HAS_CONAN_ENV:
#       此变量是公共cmake模块查找conan环境决定的变量类型
#       目的是为了支撑conan方式和非conan方式的同时处理
#   此模块主要决定以下几个环境支撑
#   [1]查找模块
#   [2]编绎目标
#   [3]调试环境变量:
#       生成VS调试环境的变量处理
#   安装:
#       自动安装库文件到安装目录
include(support_3rd_utils)

SET(SUPPORT_3RD_PKG_NAME osg)

SET(PKG_NAME ${SUPPORT_3RD_PKG_NAME})

IF(EWX_HAS_CONAN_ENV)
    message(NOTICE "${SUPPORT_3RD_PKG_NAME} Use Conan Env Mode....")
	#message(FATAL_ERROR ${CONAN_OPENSCENEGRAPH_ROOT})
	#自动适配 CONAN
    IF(NOT ${SUPPORT_3RD_PKG_NAME}_ROOT_DIR OR NOT EXISTS ${${SUPPORT_3RD_PKG_NAME}_ROOT_DIR})
        SET(${SUPPORT_3RD_PKG_NAME}_ROOT_DIR ${CONAN_OPENSCENEGRAPH_ROOT})
    ENDIF()

	find_package(OpenSceneGraph REQUIRED)
    set(osg_LIBRARY
        OPENSCENEGRAPH::osg
        OPENSCENEGRAPH::osgDB
        OPENSCENEGRAPH::OpenThreads
        OPENSCENEGRAPH::osgUtil
        OPENSCENEGRAPH::OpenThreads 
        OPENSCENEGRAPH::osgSim 
        OPENSCENEGRAPH::osgText 
        OPENSCENEGRAPH::osgFX 
        OPENSCENEGRAPH::osgGA
        OPENSCENEGRAPH::osgViewer
        OPENSCENEGRAPH::osgAnimation
        OPENSCENEGRAPH::osgdb_osgviewer
        OPENSCENEGRAPH::osgParticle
    )
    
    #调试环境变量
    MAKE_3RD_PKG_RUNTIME(${SUPPORT_3RD_PKG_NAME} ${CONAN_OPENSCENEGRAPH_ROOT})
    
	SET(PKG_NAME osg)
	SET(${PKG_NAME}_ROOT_PATH ${CONAN_OPENSCENEGRAPH_ROOT})

	IF(EXISTS ${EWX_SDK_ROOT})
		return()
	ENDIF()
    #打包使用
    if(WIN32)
		#message(NOTICE "${PKG_NAME}_ROOT_PATH=${${PKG_NAME}_ROOT_PATH} osg_dir_bin=bin ${PKG_NAME}_dir_lib=${${PKG_NAME}_dir_lib} ${PKG_NAME}_dir_bin=bin")
		#========bin======
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls...\")")
    	file(GLOB ALL_DLLS ${CONAN_OPENSCENEGRAPH_ROOT}/bin/*.dll)
        COMMON_GET_RELEASE_RUNTIME_FILES(ALL_DLLS REL_DLLS)
      	install(FILES ${REL_DLLS} 
				DESTINATION bin 
				CONFIGURATIONS Release RelWithDebInfo 
				PERMISSIONS
          			OWNER_READ OWNER_WRITE OWNER_EXECUTE 
					GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
				COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
		)
        #======plugins=====
		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${CONAN_OPENSCENEGRAPH_ROOT}/bin/osgPlugins*)
		foreach(FILE ${PLUGINLIST})
            if(IS_DIRECTORY ${FILE})
                get_filename_component(PLUGINDIRNAME ${FILE} NAME)
                file(GLOB ALL_PLUGIN_DLLS ${FILE}/*.dll)
            endif(IS_DIRECTORY ${FILE})
		endforeach(FILE)
		COMMON_GET_RELEASE_RUNTIME_FILES(ALL_PLUGIN_DLLS REL_PLUGIN_DLLS)
      	install(FILES ${REL_PLUGIN_DLLS} 
				CONFIGURATIONS Release RelWithDebInfo 
				DESTINATION bin/${PLUGINDIRNAME} 
				PERMISSIONS
          OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
					COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
		)
		#osg ext
		SET(${PKG_NAME}_EXT_BIN_PATH 
			${CONAN_OPENSCENEGRAPH_ROOT}/ext/bin
		)
		if(EXISTS ${${PKG_NAME}_EXT_BIN_PATH})
			install(CODE "MESSAGE(\"install ${PKG_NAME} ext dlls...\")")
			file(GLOB _InstallOSG_ext_dlls 
					${${PKG_NAME}_EXT_BIN_PATH}/*[!d|debug].dll 
					${${PKG_NAME}_EXT_BIN_PATH}/spatialite.dll 
					${${PKG_NAME}_EXT_BIN_PATH}/libpng.dll
			)
            INSTALL(
				FILES ${_InstallOSG_ext_dlls}
				DESTINATION bin
				CONFIGURATIONS Release
				COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
			)
		endif()
   	else(WIN32)
		message(STATUS "Install Process ${PKG_NAME} dlls From ${CONAN_OPENSCENEGRAPH_ROOT}...")
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls From ${CONAN_OPENSCENEGRAPH_ROOT}\")")
		file(GLOB REL_SOS1 ${CONAN_OPENSCENEGRAPH_ROOT}/lib/*.so)
      	file(GLOB REL_SOS2 ${CONAN_OPENSCENEGRAPH_ROOT}/lib/*.so*)
      	install(FILES ${REL_SOS1} ${REL_SOS2} 
				DESTINATION bin 
				PERMISSIONS
          OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
					COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
		)

		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${CONAN_OPENSCENEGRAPH_ROOT}/lib/osgPlugins*)
		foreach(FILE ${PLUGINLIST})
			if(IS_DIRECTORY ${FILE})
				get_filename_component(PLUGINDIRNAME ${FILE} NAME)
				file(GLOB REL_PLUGIN_DLLS1 ${FILE}/*.so)
				file(GLOB REL_PLUGIN_DLLS2 ${FILE}/*.so*)
			endif(IS_DIRECTORY ${FILE})
		endforeach(FILE)
		
      	install(FILES ${REL_PLUGIN_DLLS1} ${REL_PLUGIN_DLLS2} 
				DESTINATION bin/${PLUGINDIRNAME} 
				PERMISSIONS
          		OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
					COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
			)
   	endif(WIN32)
ELSE()
    message(NOTICE "${SUPPORT_3RD_PKG_NAME} Use Normal Package Mode....")
	
	#这里通过手动适配位置 by luli
    IF(NOT osg_ROOT_DIR OR NOT EXISTS ${osg_ROOT_DIR})
        #SET(osg_ROOT_DIR ${PROJECT_3RDPARTY_DIR}/osg-3.6.3-x64-vs2017)
		IF(WIN32)
			SEARCH_3RD_MAYBE_PATH(${SUPPORT_3RD_PKG_NAME}_ROOT_DIR 
				${PROJECT_3RDPARTY_DIR}/osg-3.6.3-x64-vs2017
				${PROJECT_3RDPARTY_DIR}/osg-3.6.3
			)
		ELSE()
			GET_THIRD_PARTY_PACKAGE_DIR_NAME("osg-3.6.3" full_package_name0)
			SEARCH_3RD_MAYBE_PATH(${SUPPORT_3RD_PKG_NAME}_ROOT_DIR 
				${PROJECT_3RDPARTY_DIR}/${full_package_name0}
			)
		ENDIF()
    ENDIF()
	
	find_package(OpenSceneGraph REQUIRED)
    set(osg_LIBRARY
        OPENSCENEGRAPH::osg
        OPENSCENEGRAPH::osgDB
        OPENSCENEGRAPH::OpenThreads
        OPENSCENEGRAPH::osgUtil
        OPENSCENEGRAPH::OpenThreads 
        OPENSCENEGRAPH::osgSim 
        OPENSCENEGRAPH::osgText 
        OPENSCENEGRAPH::osgFX 
        OPENSCENEGRAPH::osgGA
        OPENSCENEGRAPH::osgViewer
        OPENSCENEGRAPH::osgAnimation
        OPENSCENEGRAPH::osgdb_osgviewer

        OPENSCENEGRAPH::osgParticle
    )
    
	

    #调试环境变量
    MAKE_3RD_PKG_RUNTIME(${SUPPORT_3RD_PKG_NAME} ${osg_ROOT_DIR})
    
	SET(PKG_NAME osg)
	SET(${PKG_NAME}_ROOT_PATH ${osg_ROOT_DIR})
    #打包使用
    if(WIN32)
		#message(NOTICE "${PKG_NAME}_ROOT_PATH=${${PKG_NAME}_ROOT_PATH} osg_dir_bin=bin ${PKG_NAME}_dir_lib=${${PKG_NAME}_dir_lib} ${PKG_NAME}_dir_bin=bin")
		#========bin======
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls...\")")
    	file(GLOB ALL_DLLS ${${PKG_NAME}_ROOT_PATH}/bin/*.dll)
        COMMON_GET_RELEASE_RUNTIME_FILES(ALL_DLLS REL_DLLS)
      	install(FILES ${REL_DLLS} 
				DESTINATION bin 
				CONFIGURATIONS Release RelWithDebInfo 
				PERMISSIONS
          			OWNER_READ OWNER_WRITE OWNER_EXECUTE 
					GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
				COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
		)
        #======plugins=====
		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${${PKG_NAME}_ROOT_PATH}/bin/osgPlugins*)
		foreach(FILE ${PLUGINLIST})
            if(IS_DIRECTORY ${FILE})
                get_filename_component(PLUGINDIRNAME ${FILE} NAME)
                file(GLOB ALL_PLUGIN_DLLS ${FILE}/*.dll)
            endif(IS_DIRECTORY ${FILE})
		endforeach(FILE)
		COMMON_GET_RELEASE_RUNTIME_FILES(ALL_PLUGIN_DLLS REL_PLUGIN_DLLS)
      	install(FILES ${REL_PLUGIN_DLLS} 
				CONFIGURATIONS Release RelWithDebInfo 
				DESTINATION bin/${PLUGINDIRNAME} 
				PERMISSIONS
          OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
					COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
		)
		#osg ext
		SET(${PKG_NAME}_EXT_BIN_PATH 
			${${PKG_NAME}_ROOT_PATH}/ext/bin
		)
		if(EXISTS ${${PKG_NAME}_EXT_BIN_PATH})
			install(CODE "MESSAGE(\"install ${PKG_NAME} ext dlls...\")")
			file(GLOB _InstallOSG_ext_dlls 
					${${PKG_NAME}_EXT_BIN_PATH}/*[!d|debug].dll 
					${${PKG_NAME}_EXT_BIN_PATH}/spatialite.dll 
					${${PKG_NAME}_EXT_BIN_PATH}/libpng.dll
			)
            INSTALL(
				FILES ${_InstallOSG_ext_dlls}
				DESTINATION bin
				CONFIGURATIONS Release
				COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
			)
		endif()
   	else(WIN32)
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls...\")")
      	file(GLOB REL_SOS ${${PKG_NAME}_ROOT_PATH}/lib64/*.so*)
      	install(FILES ${REL_SOS} 
				DESTINATION bin 
				PERMISSIONS
          OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
					COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
		)

		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${${PKG_NAME}_ROOT_PATH}/lib64/osgPlugins*)
		foreach(FILE ${PLUGINLIST})
			if(IS_DIRECTORY ${FILE})
				get_filename_component(PLUGINDIRNAME ${FILE} NAME)
				file(GLOB REL_PLUGIN_DLLS ${FILE}/*.so*)
			endif(IS_DIRECTORY ${FILE})
		endforeach(FILE)

      	install(FILES ${REL_PLUGIN_DLLS} 
				DESTINATION bin/${PLUGINDIRNAME} 
				PERMISSIONS
          		OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
					COMPONENT ${${PKG_NAME}_INSTALL_COMPONENT}
			)
   	endif(WIN32)
ENDIF()